import { ChinaDate } from './ChinaDate'
import { GanZhi } from './ganzhi'
import { SiZhu } from './sizhu'
//用户模块
export class ClientItem {
	IsMan ?: boolean
	GenDate ?: string
	ChinaDate ?: {
		Year : number,
		MonthInner : number,
		LeapMonth : number,
		Day : number,
		Hour : number
	}
	YearGZ ?: number
	MonthGZ ?: number
	DayGZ ?: number
	HourGZ ?: number
	OtherJieQiIndex ?: number
	OtherSiZhu ?: { YearGZ : number, MonthGZ : number }
	VipName ?: string
}
/**
 * @description 使用者资料
 * @constant
 */
export class Clients {
	AddClient(theSiZhu : SiZhu) {
		var dObj : ClientItem = {
			IsMan: theSiZhu.IsMan,
			GenDate: theSiZhu.Date.toString(),
			ChinaDate: {
				Year: theSiZhu.ChinaDate.Year,
				MonthInner: theSiZhu.ChinaDate.MonthInner,
				LeapMonth: theSiZhu.ChinaDate.LeapMonth,
				Day: theSiZhu.ChinaDate.Day,
				Hour: theSiZhu.ChinaDate.Hour
			},
			YearGZ: theSiZhu.YearGZ.getHuaJiaIndex(),
			MonthGZ: theSiZhu.MonthGZ.getHuaJiaIndex(),
			DayGZ: theSiZhu.DayGZ.getHuaJiaIndex(),
			HourGZ: theSiZhu.HourGZ == null ? -1 : theSiZhu.HourGZ.getHuaJiaIndex(),
			OtherJieQiIndex: theSiZhu.OtherJieQiIndex,
			OtherSiZhu: undefined
		};
		if (theSiZhu.OtherSiZhu != null) {
			dObj.OtherSiZhu = {
				YearGZ: theSiZhu.OtherSiZhu.YearGZ.getHuaJiaIndex(),
				MonthGZ: theSiZhu.OtherSiZhu.MonthGZ.getHuaJiaIndex(),
			};
		}
		var itemcompar = function (obj1 : ClientItem, obj2 : ClientItem) {
			if (obj1.IsMan == obj2.IsMan && obj1.GenDate == obj2.GenDate && obj1.MonthGZ == obj2.MonthGZ) {
				return true;
			}
			return false;
		}
		var dtmp = window.localStorage.getItem("client0");
		window.localStorage.setItem("client0", JSON.stringify(dObj));
		if (dtmp == null) return true;
		var dtmpObj = JSON.parse(dtmp);
		if (itemcompar(dtmpObj, dObj)) return true;
		for (let xx = 1; xx < 15; xx++) {
			var dkey = "client" + xx.toString();
			var drow = window.localStorage.getItem(dkey);
			window.localStorage.setItem(dkey, dtmp);
			if (drow == null) break;
			if (itemcompar(JSON.parse(drow), dObj)) break;
			dtmp = drow;
		}
		return true;
	}
	AddVip(index : number, theName : string) {
		var dsz = this.getItem(index);
		if (dsz == null) {
			alert("没有找到这个用户的信息");
			return false;
		}
		var dObj : ClientItem = {
			IsMan: dsz.IsMan,
			GenDate: dsz.Date.toString(),
			ChinaDate: {
				Year: dsz.ChinaDate.Year,
				MonthInner: dsz.ChinaDate.MonthInner,
				LeapMonth: dsz.ChinaDate.LeapMonth,
				Day: dsz.ChinaDate.Day,
				Hour: dsz.ChinaDate.Hour
			},
			YearGZ: dsz.YearGZ.getHuaJiaIndex(),
			MonthGZ: dsz.MonthGZ.getHuaJiaIndex(),
			DayGZ: dsz.DayGZ.getHuaJiaIndex(),
			HourGZ: !dsz.HourGZ ? -1 : dsz.HourGZ.getHuaJiaIndex(),
			OtherJieQiIndex: dsz.OtherJieQiIndex,
			OtherSiZhu: undefined,
			VipName: theName
		};
		if (dsz.OtherSiZhu) {
			dObj.OtherSiZhu = {
				YearGZ: dsz.OtherSiZhu?.YearGZ.getHuaJiaIndex(),
				MonthGZ: dsz.OtherSiZhu?.MonthGZ.getHuaJiaIndex(),
			};
		}
		var itemcompar = function (obj1 : ClientItem, obj2 : ClientItem) {
			if (obj1.IsMan == obj2.IsMan && obj1.GenDate == obj2.GenDate && obj1.MonthGZ == obj2.MonthGZ) {
				return true;
			}
			return false;
		}
		var dtmp = window.localStorage.getItem("vip0");
		window.localStorage.setItem("vip0", JSON.stringify(dObj));
		if (dtmp == null) return true;
		var dtmpObj : ClientItem = JSON.parse(dtmp);
		if (itemcompar(dtmpObj, dObj)) return true;
		for (let xx = 1; xx < 100; xx++) {
			var dkey = "vip" + xx.toString();
			var drow = window.localStorage.getItem(dkey);
			window.localStorage.setItem(dkey, dtmp);
			if (drow == null) break;
			if (itemcompar(JSON.parse(drow), dObj)) break;
			dtmp = drow;
		}
		return true;
	}
	RemoveClient(index : number) {
		for (let xx = index; xx < 15; xx++) {
			var drow = window.localStorage.getItem("client" + (xx + 1).toString());
			if (drow == null) {
				window.localStorage.removeItem("client" + xx.toString());
				return true;
			};
			window.localStorage.setItem("client" + xx.toString(), drow);
		}
		return true;
	}
	RemoveVip(index : number) {
		for (let xx = index; xx < 100; xx++) {
			var drow = window.localStorage.getItem("vip" + (xx + 1).toString());
			if (drow == null) {
				window.localStorage.removeItem("vip" + xx.toString());
				return true;
			};
			window.localStorage.setItem("vip" + xx.toString(), drow);
		}
		return true;
	}
	/**
	 * @description 获取已存储的用户列表
	 * @return {Array,SiZhu}
	 */
	getArray() : SiZhu[] {
		var darray : SiZhu[] = new Array();
		for (let xx = 0; xx < 15; xx++) {
			var ditem = this.getItem(xx);
			if (ditem == null) break;
			darray.push(ditem);
		}
		return darray;
	}
	getVipArray() {
		var darray = new Array();
		for (let xx = 0; xx < 100; xx++) {
			var ditem = this.getVipItem(xx);
			if (ditem == null) break;
			darray.push(ditem);
		}
		return darray;
	}
	/**
	 * @description 获取指定位置的客户信息
	 * @param {int} index
	 * @return {SiZhu}
	 */
	getItem(index : number) : SiZhu | undefined {
		var drow = window.localStorage.getItem("client" + index.toString());
		if (drow == null) return undefined;
		var dobj : ClientItem = JSON.parse(drow);
		var ddate = new Date(Date.parse(dobj.GenDate ?? ""));
		if (!dobj.ChinaDate) throw new Error("index " + index.toString() + ", ChinaDate 对象不存在");
		var dchina = new ChinaDate(dobj.ChinaDate.Year, dobj.ChinaDate.MonthInner,
			dobj.ChinaDate.LeapMonth, dobj.ChinaDate.Day, dobj.ChinaDate.Hour);
		var disman = dobj.IsMan ?? false;
		if (!dobj.YearGZ || !dobj.MonthGZ || !dobj.DayGZ) throw new Error("index " + index.toString() + ", 年/月/日 的值不存在");
		var dyear = GanZhi.getGanzhiByHuajia(dobj.YearGZ);
		var dmonth = GanZhi.getGanzhiByHuajia(dobj.MonthGZ);
		var dday = GanZhi.getGanzhiByHuajia(dobj.DayGZ);
		var dhour = undefined;
		if (dobj.HourGZ && dobj.HourGZ > -1) dhour = GanZhi.getGanzhiByHuajia(dobj.HourGZ);
		var dsz = new SiZhu(ddate, dchina, disman, dyear, dmonth, dday, dhour);
		dsz.OtherJieQiIndex = dobj.OtherJieQiIndex;
		if (dobj.OtherSiZhu != null) {
			dyear = GanZhi.getGanzhiByHuajia(dobj.OtherSiZhu.YearGZ);
			dmonth = GanZhi.getGanzhiByHuajia(dobj.OtherSiZhu.MonthGZ);
			dsz.OtherSiZhu = new SiZhu(ddate, dchina, disman, dyear, dmonth, dday, dhour);
		}
		return dsz;
	}
	getVipItem(index : number) : SiZhu | undefined {
		var drow = window.localStorage.getItem("vip" + index.toString());
		if (!drow) return undefined;
		var dobj = JSON.parse(drow);
		var ddate = new Date(Date.parse(dobj.GenDate));
		var dchina = new ChinaDate(dobj.ChinaDate.Year, dobj.ChinaDate.MonthInner,
			dobj.ChinaDate.LeapMonth, dobj.ChinaDate.Day, dobj.ChinaDate.Hour);
		var disman = dobj.IsMan == "1";
		var dyear = GanZhi.getGanzhiByHuajia(dobj.YearGZ);
		var dmonth = GanZhi.getGanzhiByHuajia(dobj.MonthGZ);
		var dday = GanZhi.getGanzhiByHuajia(dobj.DayGZ);
		var dhour = undefined;
		if (dobj.HourGZ > -1) dhour = GanZhi.getGanzhiByHuajia(dobj.HourGZ);
		var dsz = new SiZhu(ddate, dchina, disman, dyear, dmonth, dday, dhour);
		dsz.OtherJieQiIndex = dobj.OtherJieQiIndex;
		if (dobj.OtherSiZhu) {
			dyear = GanZhi.getGanzhiByHuajia(dobj.OtherSiZhu.YearGZ);
			dmonth = GanZhi.getGanzhiByHuajia(dobj.OtherSiZhu.MonthGZ);
			dsz.OtherSiZhu = new SiZhu(ddate, dchina, disman, dyear, dmonth, dday, dhour);
		}
		dsz.VipName = dobj.VipName;
		return dsz;
	}
}

export const clients = new Clients();